package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.mapper.AddressBookMapper;
import com.reggie.pojo.AddressBook;
import com.reggie.service.AddressBookService;
import org.springframework.stereotype.Service;

/**
 * @author zhaoyuheng
 * @email 2646498606@qq.com
 * @date 2022/4/27 16:30
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void setDefault(AddressBook addressBook) {
        //查询已有的默认地址
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = this.getOne(wrapper);
        //更改默认状态
        one.setIsDefault(0);
        //保存信息
        this.updateById(one);
        //先查询地址信息
        Long id = addressBook.getId();
        AddressBook byId = this.getById(id);
        //更改默认状态
        byId.setIsDefault(1);
        //保存信息
        this.updateById(byId);
    }
}
